package ae.pcfc.etks.online.itworx.ehs.web.manager.impl;

import java.util.List;

import ae.pcfc.etks.online.itworx.ehs.web.dao.EHSDAO;
import ae.pcfc.etks.online.itworx.ehs.web.entity.DocumentType;
import ae.pcfc.etks.online.itworx.ehs.web.entity.OFCDocument;
import ae.pcfc.etks.online.itworx.ehs.web.enums.RequestStatus;
import ae.pcfc.etks.online.itworx.ehs.web.exception.DocumentTypeAlraedyExistException;
import ae.pcfc.etks.online.itworx.ehs.web.exception.DocumentTypeNotFoundException;
import ae.pcfc.etks.online.itworx.ehs.web.exception.InvalidDocumentTypeNameException;
import ae.pcfc.etks.online.itworx.ehs.web.exception.InvalidInputException;
import ae.pcfc.etks.online.itworx.ehs.web.manager.EHSManager;
import ae.pcfc.etks.online.itworx.ehs.web.util.ServiceValidator;

public class EHSManagerImpl extends BaseManagerImpl implements EHSManager {

	private EHSDAO ehsDAO;

	public EHSDAO getEhsDAO() {
		return ehsDAO;
	}

	public void setEhsDAO(EHSDAO ehsDAO) {
		this.ehsDAO = ehsDAO;
	}

	public List<DocumentType> getAlldocumentType() {
		return ehsDAO.getAllDocumentType();
	}

	public void createNewDocumentType(DocumentType documentType)
			throws DocumentTypeAlraedyExistException,
			InvalidDocumentTypeNameException, InvalidInputException {
		
		createOrUpdateDocumentType(documentType, true);
			

	}

	@Override
	public DocumentType getDocumentTypeById(Long id)
			throws DocumentTypeNotFoundException, InvalidInputException {
		DocumentType documentType = null;
		if (id > 0) {
			documentType = ehsDAO.getDocumentTypeById(id);
		} else {
			throw new InvalidInputException("Invalid ID " + id);
		}
		return documentType;
	}

	public void deleteDocumentType(Long id)
			throws DocumentTypeNotFoundException, InvalidInputException {
		DocumentType documentType = null;
		if (id > 0) {
			documentType = ehsDAO.getDocumentTypeById(id);
			if (documentType != null) {
				documentType.setStatus("I");
				ehsDAO.deleteDocumentType(documentType);
			} else {
				throw new DocumentTypeNotFoundException(
						"Document Type with this id " + id + "not found");
			}
		} else {
			throw new InvalidInputException("Invalid ID " + id);
		}
	}
	
	private void createOrUpdateDocumentType(DocumentType documentType, boolean isNew) throws DocumentTypeAlraedyExistException, InvalidDocumentTypeNameException, InvalidInputException{
		DocumentType document = null;
		if (documentType != null) {
		if (ServiceValidator.validateDocumentTypeTitle(documentType
				.getEnglishName())
				&& ServiceValidator.validateDocumentTypeTitle(documentType
						.getArabicName())) {
			document = ehsDAO.retrieveDocumentTypeByName(documentType);

			if (document == null) {
				if(isNew){
					ehsDAO.createNewDocumentType(documentType);
				}else{
					ehsDAO.updateDocumentType(documentType);
				}
					
			} else {
				throw new DocumentTypeAlraedyExistException("Document already exist");
			}
		} else {
			throw new InvalidDocumentTypeNameException("Invalid Document Type Title");
		}
		}else{
			throw new InvalidInputException("Invalid Document Type");
		}
		

	
			
	}

	public void updateDocumentType(DocumentType documentType)
			throws DocumentTypeNotFoundException, InvalidInputException,
			InvalidDocumentTypeNameException, DocumentTypeAlraedyExistException {

		//if (documentType != null) {
			// DocumentType document = null;
			// DocumentType document =
			// ehsDAO.getDocumentTypeById(documentType.getId());
			// if (null != document) {
			// if
			// (ServiceValidator.validateDocumentTypeTitle(documentType.getArabicName())
			// ||
			// ServiceValidator.validateDocumentTypeTitle(documentType.getEnglishName())){
			//ehsDAO.updateDocumentType(documentType);
			// }else{
			// throw new
			// InvalidDocumentTypeNameException("Invalid Document Type Title");
			// }

			/*
			 * } else { throw new DocumentTypeNotFoundException(
			 * "Document Type not found"); }
			 */
		/*} else {
			throw new InvalidInputException("Invalid input data");
		}*/
		createOrUpdateDocumentType(documentType, false);

	}

	//public DocumentType retrieveDocumentTypeByName(String name,Long id) {
	//	return ehsDAO.retrieveDocumentTypeByName(name,id);
	//}

	@Override
	public OFCDocument findOFCDocumentById(long id)
			throws InvalidInputException {
		if (id <= 0) {
			throw new InvalidInputException("Invalid Id");
		}
		return ehsDAO.findOFCDocumentById(id);
	}

	@Override
	public List<DocumentType> getAllDocumentTypeByOFCStatus(
			String documentName, RequestStatus requestStatus) {
		return ehsDAO.getAllDocumentTypeByOFCStatus(documentName, requestStatus);
	}

}
